/* $Id:hashtable.h jjs $ */
/*
 * ggcov - A GTK frontend for exploring gcov coverage data
 * Copyright (c) 2003-2004 Greg Banks <gnb@alphalink.com.au>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __HASHTABLE_H
#define __HASHTABLE_H

#include <glib.h>

/// Routines on hashtable Keys

template<class K> class hashtable_ops_t {
public:
    static GHashFunc hash;      ///< Calculate the hash value
    static GCompareFunc compare;///< Compare 2 keys and return 1 on equal
};

template<> GHashFunc hashtable_ops_t<char>::hash;
template<> GCompareFunc hashtable_ops_t<char>::compare;

template<> GHashFunc hashtable_ops_t<const char>::hash;
template<> GCompareFunc hashtable_ops_t<const char>::compare;

template<> GHashFunc hashtable_ops_t<void>::hash;
template<> GCompareFunc hashtable_ops_t<void>::compare;

/// GHashtable wrapper for C++

template<class K/*key*/, class T/*item*/> class hashtable {
  private:
	GHashTable* table;  ///< Data of the hashtable

  public:
    /// Function for a new hash table including targeted size
    hashtable(int size) {
		table = g_hash_table_new(
	    	hashtable_ops_t<K>::hash,
		    hashtable_ops_t<K>::compare);
    }

    /// Free data of the hashtable again
	~hashtable() {
		g_hash_table_destroy(table);
    }
    /// Insert a record into the hashtable
    /**
     *  The caller should handle the memory allocation of this data
    */
    void insert(K* key, T* value) {
    	g_hash_table_insert(table, (gpointer)key, (gpointer)value);
    }

    /// Remove a record again
    void remove(K* key) {
    	g_hash_table_remove(table, (gconstpointer)key);
    }

    /// Read a record from the hashtable
    T *operator[](K* key) {
    	return (T*)g_hash_table_lookup(table, (gconstpointer)key);
    }

    /// Return the pointer to the orginal key and the data record to free it again if needed
    bool lookup_extended(K* key, K** orig_key_ret, T **value_ret) {
    	return g_hash_table_lookup_extended(table, (gconstpointer)key,
	    	    	(gpointer *)orig_key_ret, (gpointer *)value_ret);
    }

    /// Call the handler for each record
    void foreach(void (*func)(K*, T*, void *closure), void *closure) {
    	g_hash_table_foreach(table, (GHFunc)*func, closure);
    }

    /// Call the handler and remove the record when the handler returns 1
    void foreach_remove(gboolean (*func)(K*, T*, void *closure), void *closure) {
    	g_hash_table_foreach_remove(table, (GHRFunc)func, closure);
    }

    /// Number of elements in the hashtable
    int size() const {
    	return g_hash_table_size((GHashTable*)this);
    }
};

#endif // __HASHTABLE_H
